package com.shengsheng.expschedule.service.expschedule.controller.admin;


import com.shengsheng.expschedule.base.result.R;
import com.shengsheng.expschedule.service.expschedule.entity.Major;
import com.shengsheng.expschedule.service.expschedule.service.MajorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 专业表 前端控制器
 * </p>
 *
 * @author yuan
 * @since 2021-10-02
 */
@RestController
@RequestMapping("/admin/expschedule/major")
@Api(tags = "专业管理")
@RefreshScope
public class MajorController {

    @Autowired
    private MajorService majorService;

    @ApiOperation("添加专业")
    @PostMapping("/save")
    public R save(@ApiParam(value = "专业对象", required = true) @RequestBody Major major) {
        if (majorService.save(major)) return R.ok().message("添加成功");
        return R.error().message("添加失败");
    }

    @ApiOperation("删除专业")
    @DeleteMapping("/remove/{id}")
    public R removeById(@ApiParam(value = "专业 Id", required = true) @PathVariable String id) {

        if (majorService.removeMajorById(id)) return R.ok().message("删除成功");
        return R.error().message("此专业下存在班级，删除失败！");
    }

    @ApiOperation("获取所有专业信息")
    @GetMapping("/get")
    public R getList() {
        return R.ok()
                .data("items", majorService.getListOrderBySort())
                .message("获取成功");
    }

    @ApiOperation("根据 Id 查询专业信息")
    @GetMapping("/get/{id}")
    public R getById(@ApiParam(value = "专业 Id", required = true)
                     @PathVariable String id) {
        Major major = majorService.getById(id);
        if (major == null) return R.error().message("数据不存在");
        return R.ok()
                .data("item", major)
                .message("获取成功");
    }

}

